<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Operation extends REST_Controller
{
    protected $rules = array(
        "add_banner" => array(
            array(
                'field' => 'Title',
                'label' => '标题',
                'rules' => 'trim|required'
            ),
            array(
                'field' => 'Photo',
                'label' => '图片',
                'rules' => 'trim|required|numeric'
            ),
        ),
        "add_qualification" => array(
            array(
                'field' => 'Title',
                'label' => '标题',
                'rules' => 'trim|required'
            ),
            array(
                'field' => 'Photo',
                'label' => '图片',
                'rules' => 'trim|required'
            ),
        ),
        "add_equipment" => array(
            array(
                'field' => 'Title',
                'label' => '标题',
                'rules' => 'trim|required'
            ),
            array(
                'field' => 'Photo',
                'label' => '图片',
                'rules' => 'trim|required'
            ),
        ),
    );

    private $rdata = array();

    function __construct()
    {
        session_start();
        parent::__construct();

        $this->load->model('common_model', 'common', 'default');
        $this->load->model('banner_model', 'banner');
        $this->load->model('upload_model', 'upload');
        $this->load->model('qualification_model', 'qualification');//资质表
        $this->load->model('equipment_model', 'equipment');//设备表

        $this->returnInfo = lang('returnInfo');
        $this->rdata['returnNo'] = '400';
        $this->rdata['returnInfo'] = '请求操作失败';
        $this->rdata['returnTime'] = time();
        $this->rdata['Secure'] = JSON_SECURE;
        $this->rdata['Content2'] = null;
        $this->rdata['Content'] = null;
        $this->rdata['returnData'] = null;
        $this->load->helpers('uisite_helper');
    }

    /**
     * 企业banner-添加
     * title(必填) link photo(单)
     */
    public function add_banner_post()
    {
        $data = $this->post();
        $this->form_validation->set_rules($this->rules['add_banner']);
        if ($this->form_validation->run() == FALSE) {
            // 返回失败
            $this->rdata['ReturnCode'] = '400';
            $this->rdata['ReturnInfo'] = validation_errors();
            $this->response($this->rdata);
        }
        if ($this->uinfo['type'] != 1) {
            // 返回失败
            $this->rdata['ReturnCode'] = '400';
            $this->rdata['ReturnInfo'] = '非主账号无法提交';
            $this->response($this->rdata);
        }
        $create_data = array(
            'uid' => $this->uinfo['id'],
            'title' => $data['Title'],
            'link' => $data['Link'],
            'photo' => $data['Photo'],
            'timeline' => time()
        );
        if ($this->banner->insertInfo($create_data) == TRUE) {
            $this->rdata['ReturnCode'] = '200';
            $this->rdata['ReturnInfo'] = '提交成功';
        } else {
            $this->rdata['ReturnCode'] = '400';
            $this->rdata['ReturnInfo'] = '提交失败';
        }
        $this->response($this->rdata);
    }

    /*
     * 企业banner-列表
     * limit page
     */
    public function banner_list_get()
    {
        if ($this->uinfo['type'] != 1) {
            // 非主账号返回失败
            $this->rdata['ReturnCode'] = '400';
            $this->rdata['ReturnInfo'] = '非主账号无法查询';
            $this->response($this->rdata);
        }

        $data = $this->get();
        $page=isset($data['PageNo'])?$data['PageNo']:1;
        $limit=isset($data['Limit'])?$data['Limit']:10;
        $page = $limit*($page-1);
        $where = array('uid' => $this->uinfo['id']);
        $result_t = $this->db->select('id,title,link,photo,timeline')
            ->from("boc_banner")
            ->where($where)
            ->order_by('timeline', 'desc')
            ->limit($limit, $page)
            ->get();
        $lists = $result_t->result_array();
        foreach ($lists as $k => $v) {
            $photo_info = $this->upload->get_one(array('id' => $v['photo']), 'url');
            if ($photo_info) {
                $lists[$k]['thumb'] = UPLOAD_URL . $photo_info['url'];
            } else {
                $lists[$k]['thumb'] = '';
            }
        }
        $result['lists'] = $lists;
        $result['total'] = $this->banner->countLog($where);

        $this->rdata['ReturnCode'] = '200';
        $this->rdata['ReturnInfo'] = '获取成功';
        $this->rdata['Content'] = $result;
        $this->response($this->rdata);
    }

    /*
     * 资质列表-添加
     * title(必填) content photo(多)
     */
    public function add_qualification_post()
    {
        $data = $this->post();
        $this->form_validation->set_rules($this->rules['add_qualification']);
        if ($this->form_validation->run() == FALSE) {
            // 未通过表单验证返回失败
            $this->rdata['ReturnCode'] = '400';
            $this->rdata['ReturnInfo'] = validation_errors();
            $this->response($this->rdata);
        }
        if ($this->uinfo['type'] != 1) {
            // 非主账号返回失败
            $this->rdata['ReturnCode'] = '400';
            $this->rdata['ReturnInfo'] = '非主账号无法提交';
            $this->response($this->rdata);
        }
        $create_data = array(
            'uid' => $this->uinfo['id'],
            'title' => $data['Title'],
            'content' => $data['Content'],
            'photo' => $data['Photo'],
            'timeline' => time()
        );
        if ($this->qualification->insertInfo($create_data) == TRUE) {
            $this->rdata['ReturnCode'] = '200';
            $this->rdata['ReturnInfo'] = '提交成功';
        } else {
            $this->rdata['ReturnCode'] = '400';
            $this->rdata['ReturnInfo'] = '提交失败';
        }
        $this->response($this->rdata);
    }

    /*
     * 资质列表-列表
     * limit page
     */
    public function qualification_list_get() {
        if ($this->uinfo['type'] != 1) {
            // 非主账号返回失败
            $this->rdata['ReturnCode'] = '400';
            $this->rdata['ReturnInfo'] = '非主账号无法查询';
            $this->response($this->rdata);
        }
        $data = $this->get();
        $page=isset($data['PageNo'])?$data['PageNo']:1;
        $limit=isset($data['Limit'])?$data['Limit']:10;
        $page = $limit*($page-1);
        $where = array('uid' => $this->uinfo['id']);
        $result_t = $this->db->select('id,title,content,photo,timeline')
            ->from("boc_qualification")
            ->where($where)
            ->order_by('timeline', 'desc')
            ->limit($limit, $page)
            ->get();
        $lists = $result_t->result_array();
        foreach ($lists as $k => $v) {
            $photo_info = UPLOAD_URL.tag_photo($v['photo']);
            if (UPLOAD_PATH.tag_photo($v['photo'])) {
                $lists[$k]['thumb'] = $photo_info;
            } else {
                $lists[$k]['thumb'] = '';
            }
        }
        $result['lists'] = $lists;
        $result['total'] = $this->qualification->countLog($where);

        $this->rdata['ReturnCode'] = '200';
        $this->rdata['ReturnInfo'] = '获取成功';
        $this->rdata['Content'] = $result;
        $this->response($this->rdata);
    }

    /**
     * 设备清单-添加
     * title(必填) content photo(多)
     */
    public function add_equipment_post() {
        $data = $this->post();
        $this->form_validation->set_rules($this->rules['add_equipment']);
        if ($this->form_validation->run() == FALSE) {
            // 未通过表单验证返回失败
            $this->rdata['ReturnCode'] = '400';
            $this->rdata['ReturnInfo'] = validation_errors();
            $this->response($this->rdata);
        }
        if ($this->uinfo['type'] != 1) {
            // 非主账号返回失败
            $this->rdata['ReturnCode'] = '400';
            $this->rdata['ReturnInfo'] = '非主账号无法提交';
            $this->response($this->rdata);
        }
        $create_data = array(
            'uid' => $this->uinfo['id'],
            'title' => $data['Title'],
            'content' => $data['Content'],
            'photo' => $data['Photo'],
            'timeline' => time()
        );
        if ($this->equipment->insertInfo($create_data) == TRUE) {
            $this->rdata['ReturnCode'] = '200';
            $this->rdata['ReturnInfo'] = '提交成功';
        } else {
            $this->rdata['ReturnCode'] = '400';
            $this->rdata['ReturnInfo'] = '提交失败';
        }
        $this->response($this->rdata);
    }

    /*
     * 设备清单-列表
     * limit page
     */
    public function equipment_list_get() {
        if ($this->uinfo['type'] != 1) {
            // 非主账号返回失败
            $this->rdata['ReturnCode'] = '400';
            $this->rdata['ReturnInfo'] = '非主账号无法查询';
            $this->response($this->rdata);
        }

        $data = $this->get();
        $page=isset($data['PageNo'])?$data['PageNo']:1;
        $limit=isset($data['Limit'])?$data['Limit']:10;
        $page = $limit*($page-1);
        $where = array('uid' => $this->uinfo['id']);
        $result_t = $this->db->select('id,title,content,photo,timeline')
            ->from("boc_equipment")
            ->where($where)
            ->order_by('timeline', 'desc')
            ->limit($limit, $page)
            ->get();
        $lists = $result_t->result_array();
        foreach ($lists as $k => $v) {
            $photo_info = UPLOAD_URL.tag_photo($v['photo']);
            if (UPLOAD_PATH.tag_photo($v['photo'])) {
                $lists[$k]['thumb'] = $photo_info;
            } else {
                $lists[$k]['thumb'] = '';
            }
        }
        $result['lists'] = $lists;
        $result['total'] = $this->equipment->countLog($where);
        $this->rdata['ReturnCode'] = '200';
        $this->rdata['ReturnInfo'] = '获取成功';
        $this->rdata['Content'] = $result;
        $this->response($this->rdata);
    }


}